<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/3/2
 * Time: 14:58
 */
declare(strict_types=1);

namespace App\Service;

use App\Model\Dao\EventOrderDao;
use App\Model\Dao\MchConfigDao;
use App\Model\Dao\MchInfoDao;
use App\Model\Dao\StatMonthlyEventDao;
use App\Model\Dao\StatMonthlyTradeChannelDao;
use App\Model\Dao\StatMonthlyTradeDao;
use App\Model\Dao\StatMonthlyTransDao;
use App\Model\Dao\StatDailyTradeChannelDao;
use App\Model\Dao\StatDailyTransDao;
use App\Model\Dao\StatDailyEventDao;
use App\Model\Dao\StatDailyTradeDao;
use App\Model\Dao\SysAdminDao;
use App\Model\Dao\TradeOrderDetailDao;
use App\Model\Dao\TradeOrderDisDao;
use App\Model\Dao\TradeOrderFrozenDao;
use App\Model\Dao\TradeOrderRefundDao;
use App\Model\Dao\TradeOrderThawDao;
use App\Model\Dao\TradeTransDao;
use App\Utils\CustomUtils;
use Hyperf\Di\Annotation\Inject;

class StatMchService
{

    /**
     * @Inject()
     * @var MchInfoDao
     */
    private $MchInfoDao;

    /**
     * @Inject()
     * @var MchConfigDao
     */
    private $MchConfigDao;

    /**
     * @Inject()
     * @var TradeOrderDetailDao
     */
    private $TradeOrderDetailDao;

    /**
     * @Inject()
     * @var TradeOrderDisDao
     */
    private $TradeOrderDisDao;

    /**
     * @Inject()
     * @var TradeOrderRefundDao
     */
    private $TradeOrderRefundDao;

    /**
     * @Inject()
     * @var TradeOrderFrozenDao
     */
    private $TradeOrderFrozenDao;

    /**
     * @Inject()
     * @var TradeOrderThawDao
     */
    private $TradeOrderThawDao;

    /**
     * @Inject()
     * @var TradeTransDao
     */
    private $TradeTransDao;

    /**
     * @Inject()
     * @var EventOrderDao
     */
    private $EventOrderDao;

    /**
     * @Inject()
     * @var StatMonthlyTradeDao
     */
    private $StatMonthlyTradeDao;

    /**
     * @Inject()
     * @var StatDailyTradeDao
     */
    private $StatDailyTradeDao;

    /**
     * @Inject()
     * @var StatMonthlyTradeChannelDao
     */
    private $StatMonthlyTradeChannelDao;

    /**
     * @Inject()
     * @var StatDailyTradeChannelDao
     */
    private $StatDailyTradeChannelDao;

    /**
     * @Inject()
     * @var StatMonthlyTransDao
     */
    private $StatMonthlyTransDao;

    /**
     * @Inject()
     * @var StatDailyTransDao
     */
    private $StatDailyTransDao;

    /**
     * @Inject()
     * @var StatMonthlyEventDao
     */
    private $StatMonthlyEventDao;

    /**
     * @Inject()
     * @var StatDailyEventDao
     */
    private $StatDailyEventDao;

    /**
     * @Inject()
     * @var SysAdminDao
     */
    private $SysAdminDao;

    /**
     * @param $startTime 开始时间
     * @param $endTime 结束时间
     * @param $statMonth 统计月份
     * @param array $mchNos 商户id，可以单独统计某个商户
     * @param bool $isForce 是否强制，是的话会把已经生成的软删除再生成
     */
    public function stat($startTime, $endTime, $statMonth, $mchNos = [], $isForce = false) {

        try {
            // 先取商户
            $mchList = null;
            if (empty($mchNos)) {
                $mchList = $this->MchInfoDao->getAll();
            } else {
                $mchList = $this->MchInfoDao->getByMchNos($mchNos);
            }
            foreach ($mchList as $mch) {

                $this->statTrans($mch, $statMonth, $startTime, $endTime, $isForce);

                $this->statEventOrder($mch, $statMonth, $startTime, $endTime, $isForce);

                $this->statTrade($mch, $statMonth, $startTime, $endTime, $isForce);
            }
        } catch (\Throwable $e) {
            CustomUtils::xzxDebug($e->getMessage());
        }

    }


    /**
     * @param $startTime 开始时间
     * @param $endTime 结束时间
     * @param $statDate 统计日期
     * @param array $mchNos 商户id，可以单独统计某个商户
     * @param bool $isForce 是否强制，是的话会把已经生成的软删除再生成
     */
    public function statDaily($startTime, $endTime, $statDate, $mchNos = [], $isForce = false) {
        // CustomUtils::xzxDebug($startTime);
        // CustomUtils::xzxDebug($endTime);
        // CustomUtils::xzxDebug($statDate);
        // CustomUtils::xzxDebug($mchNos);
        // CustomUtils::xzxDebug($isForce);

        try {
            // 先取商户
            $mchList = null;
            if (empty($mchNos)) {
                $mchList = $this->MchInfoDao->getAll();
            } else {
                $mchList = $this->MchInfoDao->getByMchNos($mchNos);
            }
            CustomUtils::xzxDebug($mchList);
            foreach ($mchList as $mch) {

                $this->statTransDaily($mch, $statDate, $startTime, $endTime, $isForce);

                $this->statEventOrderDaily($mch, $statDate, $startTime, $endTime, $isForce);

                $this->statTradeDaily($mch, $statDate, $startTime, $endTime, $isForce);
            }
        } catch (\Throwable $e) {
            CustomUtils::xzxDebug($e->getMessage());
        }

    }

    /**
     * @param $mch
     * @param $statMonth
     * @param $startTime
     * @param $endTime
     * @param $isForce
     * 提现/转账的统计
     */
    private function statTrans($mch, $statMonth, $startTime, $endTime, $isForce) {
        $isTrans = $this->StatMonthlyTransDao->getWhere([
            'mchId'         => $mch['id'],
            'statMonth'     => $statMonth,
            'delFlag'       => 0
        ]);

        if ($isForce && $isTrans) {
            // 强制成功且有数据就删除
            $this->StatMonthlyTransDao->del($isTrans['id']);
        } else if (!$isForce && $isTrans) {
            // 有数据就不管了
            return;
        }
        $sumTradeTrans = $this->TradeTransDao->getSumByChannelIdAndMchIdAndHandleStatusAndStartTimeAndEndTime($mch['id'], [3], $startTime, $endTime);

        // 取type做key
        $sumTradeTrans = array_column($sumTradeTrans, null, "type");
        $data = [
            'id'                => CustomUtils::createUUID(),
            'mchId'             => $mch['id'],
            'mchNo'             => $mch['mchNo'],
            'withdrawFee'       => isset($sumTradeTrans['1']) ? $sumTradeTrans['1']['transFee'] : 0,
            'transFee'          => isset($sumTradeTrans['2']) ? $sumTradeTrans['2']['transFee'] : 0,
            'withdrawAmount'    => isset($sumTradeTrans['1']) ? $sumTradeTrans['1']['transAmount'] : 0,
            'transAmount'       => isset($sumTradeTrans['2']) ? $sumTradeTrans['2']['transAmount'] : 0,
            'statMonth'         => $statMonth,
            'statStartTime'     => $startTime,
            'statEndTime'       => $endTime,
            'createTime'        => time()
        ];
        $this->StatMonthlyTransDao->add($data);
    }

    /**
     * @param $mch
     * @param $statDate
     * @param $startTime
     * @param $endTime
     * @param $isForce
     * 提现/转账的统计
     */
    private function statTransDaily($mch, $statDate, $startTime, $endTime, $isForce) {
        $isTrans = $this->StatDailyTransDao->getWhere([
            'mchId'         => $mch['id'],
            'statDate'     => $statDate,
            'delFlag'       => 0
        ]);

        if ($isForce && $isTrans) {
            // 强制成功且有数据就删除
            $this->StatDailyTransDao->del($isTrans['id']);
        } else if (!$isForce && $isTrans) {
            // 有数据就不管了
            return;
        }
        $sumTradeTrans = $this->TradeTransDao->getSumByChannelIdAndMchIdAndHandleStatusAndStartTimeAndEndTime($mch['id'], [3], $startTime, $endTime);

        // 取type做key
        $sumTradeTrans = array_column($sumTradeTrans, null, "type");
        $data = [
            'id'                => CustomUtils::createUUID(),
            'mchId'             => $mch['id'],
            'mchNo'             => $mch['mchNo'],
            'withdrawFee'       => isset($sumTradeTrans['1']) ? $sumTradeTrans['1']['transFee'] : 0,
            'transFee'          => isset($sumTradeTrans['2']) ? $sumTradeTrans['2']['transFee'] : 0,
            'withdrawAmount'    => isset($sumTradeTrans['1']) ? $sumTradeTrans['1']['transAmount'] : 0,
            'transAmount'       => isset($sumTradeTrans['2']) ? $sumTradeTrans['2']['transAmount'] : 0,
            'statDate'         => $statDate,
            'statStartTime'     => $startTime,
            'statEndTime'       => $endTime,
            'createTime'        => time()
        ];
        $this->StatDailyTransDao->add($data);
    }

    /**
     * @param $mch
     * @param $statMonth
     * @param $startTime
     * @param $endTime
     * @param $isForce
     * 事件订单
     */
    private function statEventOrder($mch, $statMonth, $startTime, $endTime, $isForce) {
        $isEvent = $this->StatMonthlyEventDao->getWhere([
            'mchId'         => $mch['id'],
            'statMonth'     => $statMonth,
            'delFlag'       => 0
        ]);

        if ($isForce && !!$isEvent) {
            // 强制成功且有数据就删除
            $this->StatMonthlyEventDao->del($isEvent['id']);
        } else if (!$isForce && !!$isEvent) {
            // 有数据就不管了
            return;
        }
        $sumEvent = $this->EventOrderDao->getSumByChannelIdAndMchIdAndStartTimeAndEndTime($mch['id'], [3], $startTime, $endTime);

        // 取type做key
        $sumEvent = array_column($sumEvent, null, "type");
        $data = [
            'id'                   => CustomUtils::createUUID(),
            'mchId'                => $mch['id'],
            'mchNo'                => $mch['mchNo'],
            'eventFreezeAmount'    => isset($sumEvent['8001']) ? $sumEvent['8001']['amount'] : 0,
            'eventDeductionAmount' => isset($sumEvent['8002']) ? $sumEvent['8002']['amount'] : 0,
            'eventRechargeAmount'  => isset($sumEvent['8003']) ? $sumEvent['8003']['amount'] : 0,
            'eventThawAmount'      => isset($sumEvent['8004']) ? $sumEvent['8004']['amount'] : 0,
            'statMonth'            => $statMonth,
            'statStartTime'        => $startTime,
            'statEndTime'          => $endTime,
            'createTime'           => time()
        ];
        $this->StatMonthlyEventDao->add($data);
    }

    /**
     * @param $mch
     * @param $statDate
     * @param $startTime
     * @param $endTime
     * @param $isForce
     * 事件订单
     */
    private function statEventOrderDaily($mch, $statDate, $startTime, $endTime, $isForce) {
        $isEvent = $this->StatDailyEventDao->getWhere([
            'mchId'         => $mch['id'],
            'statDate'     => $statDate,
            'delFlag'       => 0
        ]);

        if ($isForce && !!$isEvent) {
            // 强制成功且有数据就删除
            $this->StatDailyEventDao->del($isEvent['id']);
        } else if (!$isForce && !!$isEvent) {
            // 有数据就不管了
            return;
        }
        $sumEvent = $this->EventOrderDao->getSumByChannelIdAndMchIdAndStartTimeAndEndTime($mch['id'], [3], $startTime, $endTime);

        // 取type做key
        $sumEvent = array_column($sumEvent, null, "type");
        $data = [
            'id'                   => CustomUtils::createUUID(),
            'mchId'                => $mch['id'],
            'mchNo'                => $mch['mchNo'],
            'eventFreezeAmount'    => isset($sumEvent['8001']) ? $sumEvent['8001']['amount'] : 0,
            'eventDeductionAmount' => isset($sumEvent['8002']) ? $sumEvent['8002']['amount'] : 0,
            'eventRechargeAmount'  => isset($sumEvent['8003']) ? $sumEvent['8003']['amount'] : 0,
            'eventThawAmount'      => isset($sumEvent['8004']) ? $sumEvent['8004']['amount'] : 0,
            'statDate'            => $statDate,
            'statStartTime'        => $startTime,
            'statEndTime'          => $endTime,
            'createTime'           => time()
        ];
        $this->StatDailyEventDao->add($data);
    }

    /**
     * @param $mch
     * @param $statMonth
     * @param $startTime
     * @param $endTime
     * @param $isForce
     * 商户渠道统计
     */
    private function statTrade($mch, $statMonth, $startTime, $endTime, $isForce) {
        // 取配置
        $config = $this->MchConfigDao->getWhere(['mchId' => $mch['id']]);
        if (empty($config)) {
            CustomUtils::xzxDebug("{$mch['mchNo']} 未对商户配置");
            return;
        }

        // 先来搞商户的
        $isStatTrade = $this->StatMonthlyTradeDao->getWhere([
            'mchId'         => $mch['id'],
            'statMonth'     => $statMonth,
            'delFlag'       => 0
        ]);
        if ($isForce && $isStatTrade) {
            // 强制成功且有数据就删除
            $this->StatMonthlyTradeDao->del($isStatTrade['id']);
        } else if (!$isForce && $isStatTrade) {
            // 有数据就不管了
            return;
        }

        $parentSalesId = '';
        if (!empty($mch['salesId'])) {
            $sales = $this->SysAdminDao->get($mch['salesId']);
            $parentSalesId = $sales['parentId'];
        }
        // 取数据
        $sum        = $this->TradeOrderDetailDao->getSumByTransStatusAndStartTimeAndEndTime($mch['id'], null, [6], $startTime, $endTime);
        $sumRisk    = $this->TradeOrderDetailDao->getCountByTransStatusAndStartTimeAndEndTime($mch['id'], null, [4], $startTime, $endTime);
        $sumFee     = $this->TradeOrderDetailDao->getSumByTransStatusAndStartTimeAndEndTime($mch['id'], null, [], $startTime, $endTime);
        $sumFrozen  = $this->TradeOrderFrozenDao->getSumByHandleStatusAndStartTimeAndEndTime($mch['id'], null, [3], $startTime, $endTime);
        $sumThaw    = $this->TradeOrderThawDao->getSumByHandleStatusAndStartTimeAndEndTime($mch['id'], null, [3], $startTime, $endTime);
        $sumRefund  = $this->TradeOrderRefundDao->getSumByHandleStatusAndStartTimeAndEndTime($mch['id'], null, [3], $startTime, $endTime);
        $sumDis     = $this->TradeOrderDisDao->getSumByHandleStatusAndStartTimeAndEndTime($mch['id'], null, [3], $startTime, $endTime);

        // 写统计表
        $data = [
            'id'                     => CustomUtils::createUUID(),
            'mchId'                  => $mch['id'],
            'mchNo'                  => $mch['mchNo'],
            'mchName'                => $mch['mchName'],
            'settleCurrency'         => $config['settleCurrency'],

            'successCount'           => isset($sum['totalCount']) ? $sum['totalCount'] : 0,
            'riskCount'              => isset($sumRisk['totalCount']) ? $sumRisk['totalCount'] : 0,
            'totalCount'             => isset($sumFee['totalCount']) ? $sumFee['totalCount'] : 0,
            'disCount'               => isset($sumDis['transCount']) ? $sumDis['transCount'] : 0,
            'refundCount'            => isset($sumRefund['transCount']) ? $sumRefund['transCount'] : 0,

            'settleAmount'           => isset($sumFee['settleAmount']) ? $sumFee['settleAmount'] : 0,
            'successSettleAmount'    => isset($sum['settleAmount']) ? $sum['settleAmount'] : 0,
            'bondAmount'             => isset($sum['bondAmount']) ? $sum['bondAmount'] : 0,

            'refundSettleAmount'     => isset($sumRefund['transSettleAmount']) ? $sumRefund['transSettleAmount'] : 0,
            'frozenSettleAmount'     => isset($sumFrozen['transSettleAmount']) ? $sumFrozen['transSettleAmount'] : 0,
            'disSettleAmount'        => isset($sumDis['transSettleAmount']) ? $sumDis['transSettleAmount'] : 0,
            'thawSettleAmount'       => isset($sumThaw['transSettleAmount']) ? $sumThaw['transSettleAmount'] : 0,

            'perFee'                 => isset($sumFee['perFee']) ? $sumFee['perFee'] : 0,
            'serviceFee'             => isset($sumFee['serviceFee']) ? $sumFee['serviceFee'] : 0,
            'refundFee'              => isset($sumFee['refundFee']) ? $sumFee['refundFee'] : 0,
            'disFee'                 => isset($sumFee['disFee']) ? $sumFee['disFee'] : 0,
            'disLookIntoFee'         => isset($sumFee['disLookIntoFee']) ? $sumFee['disLookIntoFee'] : 0,
            'transFailFee'           => isset($sumFee['transFailFee']) ? $sumFee['transFailFee'] : 0,
            'threeDJudgeFee'         => isset($sumFee['threeDJudgeFee']) ? $sumFee['threeDJudgeFee'] : 0,
            'threeDFee'              => isset($sumFee['threeDFee']) ? $sumFee['threeDFee'] : 0,

            'salesLevel1'            => $parentSalesId,
            'salesLevel2'            => $mch['salesId'],

            'salesCommission1'       => isset($sum['salesCommission1']) ? $sum['salesCommission1'] : 0,
            'salesCommission2'       => isset($sum['salesCommission2']) ? $sum['salesCommission2'] : 0,

            'statMonth'              => $statMonth,
            'statStartTime'          => $startTime,
            'statEndTime'            => $endTime,
            'delFlag'                => 0,
            'createTime'             => time()
        ];
        // 算一下几个百分比
        $data['successRate'] = $data['totalCount'] == 0 ? 10000 : round($data['successCount'] / $data['totalCount'] * 10000);
        $data['refundRate'] = $data['totalCount'] == 0 ? 0 : round($data['refundCount'] / $data['totalCount'] * 10000);
        $data['disRate'] = $data['totalCount'] == 0 ? 0 : round($data['disCount'] / $data['totalCount'] * 10000);
        $this->StatMonthlyTradeDao->add($data);


        // 取商户通道
        $mchChannelList = $this->MchInfoDao->getMchChannel($mch['mchNo']);
        foreach ($mchChannelList as $channel) {

            $isStat = $this->StatMonthlyTradeChannelDao->getWhere([
                'mchId'     => $mch['id'],
                'channelId' => $channel['id'],
                'statMonth' => $statMonth,
                'delFlag' => 0
            ]);

            if ($isForce && $isStat) {
                // 强制成功且有数据就删除
                $this->StatMonthlyTradeChannelDao->del($isStat['id']);
            } else if (!$isForce && $isStat) {
                // 有数据就不管了
                continue;
            }

            // 取数据
            $sum = $this->TradeOrderDetailDao->getSumByTransStatusAndStartTimeAndEndTime($mch['id'], $channel['id'], [6], $startTime, $endTime);
            $sumFee = $this->TradeOrderDetailDao->getSumByTransStatusAndStartTimeAndEndTime($mch['id'], $channel['id'], [], $startTime, $endTime);

            // 写统计表
            $data = [
                'id'                     => CustomUtils::createUUID(),
                'mchId'                  => $mch['id'],
                'mchNo'                  => $mch['mchNo'],
                'mchName'                => $mch['mchName'],
                'channelId'              => $channel['id'],
                'channelName'            => $channel['channelName'],
                'settleCurrency'         => $config['settleCurrency'],

                'settleAmount'           => isset($sum['settleAmount']) ? $sum['settleAmount'] : 0,
                'serviceFee'             => isset($sumFee['serviceFee']) ? $sumFee['serviceFee'] : 0,

                'salesLevel1'            => $parentSalesId,
                'salesLevel2'            => $mch['salesId'],

                'salesCommission1'       => isset($sum['salesCommission1']) ? $sum['salesCommission1'] : 0,
                'salesCommission2'       => isset($sum['salesCommission2']) ? $sum['salesCommission2'] : 0,
                'channelSettleCurrency'  => $channel['settleCurrency'],

                'statMonth'              => $statMonth,
                'statStartTime'          => $startTime,
                'statEndTime'            => $endTime,
                'delFlag'                => 0,
                'createTime'             => time()
            ];
            $this->StatMonthlyTradeChannelDao->add($data);
        }
    }


    /**
     * @param $mch
     * @param $statDate
     * @param $startTime
     * @param $endTime
     * @param $isForce
     * 商户渠道统计
     */
    private function statTradeDaily($mch, $statDate, $startTime, $endTime, $isForce) {
        // 取配置
        $config = $this->MchConfigDao->getWhere(['mchId' => $mch['id']]);
        if (empty($config)) {
            CustomUtils::xzxDebug("{$mch['mchNo']} 未对商户配置");
            return;
        }

        // 先来搞商户的
        $isStatTrade = $this->StatDailyTradeDao->getWhere([
            'mchId'         => $mch['id'],
            'statDate'     => $statDate,
            'delFlag'       => 0
        ]);
        if ($isForce && $isStatTrade) {
            // 强制成功且有数据就删除
            $this->StatDailyTradeDao->del($isStatTrade['id']);
        } else if (!$isForce && $isStatTrade) {
            // 有数据就不管了
            return;
        }

        $parentSalesId = '';
        if (!empty($mch['salesId'])) {
            $sales = $this->SysAdminDao->get($mch['salesId']);
            $parentSalesId = $sales['parentId'];
        }
        // 取数据
        $sum        = $this->TradeOrderDetailDao->getSumByTransStatusAndStartTimeAndEndTime($mch['id'], null, [6], $startTime, $endTime);
        $sumRisk    = $this->TradeOrderDetailDao->getCountByTransStatusAndStartTimeAndEndTime($mch['id'], null, [4], $startTime, $endTime);
        $sumFee     = $this->TradeOrderDetailDao->getSumByTransStatusAndStartTimeAndEndTime($mch['id'], null, [], $startTime, $endTime);
        $sumFrozen  = $this->TradeOrderFrozenDao->getSumByHandleStatusAndStartTimeAndEndTime($mch['id'], null, [3], $startTime, $endTime);
        $sumThaw    = $this->TradeOrderThawDao->getSumByHandleStatusAndStartTimeAndEndTime($mch['id'], null, [3], $startTime, $endTime);
        $sumRefund  = $this->TradeOrderRefundDao->getSumByHandleStatusAndStartTimeAndEndTime($mch['id'], null, [3], $startTime, $endTime);
        $sumDis     = $this->TradeOrderDisDao->getSumByHandleStatusAndStartTimeAndEndTime($mch['id'], null, [3], $startTime, $endTime);

        // 写统计表
        $data = [
            'id'                     => CustomUtils::createUUID(),
            'mchId'                  => $mch['id'],
            'mchNo'                  => $mch['mchNo'],
            'mchName'                => $mch['mchName'],
            'settleCurrency'         => $config['settleCurrency'],

            'successCount'           => isset($sum['totalCount']) ? $sum['totalCount'] : 0,
            'riskCount'              => isset($sumRisk['totalCount']) ? $sumRisk['totalCount'] : 0,
            'totalCount'             => isset($sumFee['totalCount']) ? $sumFee['totalCount'] : 0,
            'disCount'               => isset($sumDis['transCount']) ? $sumDis['transCount'] : 0,
            'refundCount'            => isset($sumRefund['transCount']) ? $sumRefund['transCount'] : 0,

            'settleAmount'           => isset($sumFee['settleAmount']) ? $sumFee['settleAmount'] : 0,
            'successSettleAmount'    => isset($sum['settleAmount']) ? $sum['settleAmount'] : 0,
            'bondAmount'             => isset($sum['bondAmount']) ? $sum['bondAmount'] : 0,

            'refundSettleAmount'     => isset($sumRefund['transSettleAmount']) ? $sumRefund['transSettleAmount'] : 0,
            'frozenSettleAmount'     => isset($sumFrozen['transSettleAmount']) ? $sumFrozen['transSettleAmount'] : 0,
            'disSettleAmount'        => isset($sumDis['transSettleAmount']) ? $sumDis['transSettleAmount'] : 0,
            'thawSettleAmount'       => isset($sumThaw['transSettleAmount']) ? $sumThaw['transSettleAmount'] : 0,

            'perFee'                 => isset($sumFee['perFee']) ? $sumFee['perFee'] : 0,
            'serviceFee'             => isset($sumFee['serviceFee']) ? $sumFee['serviceFee'] : 0,
            'refundFee'              => isset($sumFee['refundFee']) ? $sumFee['refundFee'] : 0,
            'disFee'                 => isset($sumFee['disFee']) ? $sumFee['disFee'] : 0,
            'disLookIntoFee'         => isset($sumFee['disLookIntoFee']) ? $sumFee['disLookIntoFee'] : 0,
            'transFailFee'           => isset($sumFee['transFailFee']) ? $sumFee['transFailFee'] : 0,
            'threeDJudgeFee'         => isset($sumFee['threeDJudgeFee']) ? $sumFee['threeDJudgeFee'] : 0,
            'threeDFee'              => isset($sumFee['threeDFee']) ? $sumFee['threeDFee'] : 0,

            'salesLevel1'            => $parentSalesId,
            'salesLevel2'            => $mch['salesId'],

            'salesCommission1'       => isset($sum['salesCommission1']) ? $sum['salesCommission1'] : 0,
            'salesCommission2'       => isset($sum['salesCommission2']) ? $sum['salesCommission2'] : 0,

            'statDate'              => $statDate,
            'statStartTime'          => $startTime,
            'statEndTime'            => $endTime,
            'delFlag'                => 0,
            'createTime'             => time()
        ];
        // 算一下几个百分比
        $data['successRate'] = $data['totalCount'] == 0 ? 10000 : round($data['successCount'] / $data['totalCount'] * 10000);
        $data['refundRate'] = $data['totalCount'] == 0 ? 0 : round($data['refundCount'] / $data['totalCount'] * 10000);
        $data['disRate'] = $data['totalCount'] == 0 ? 0 : round($data['disCount'] / $data['totalCount'] * 10000);
        $this->StatDailyTradeDao->add($data);


        // 取商户通道
        $mchChannelList = $this->MchInfoDao->getMchChannel($mch['mchNo']);
        foreach ($mchChannelList as $channel) {

            $isStat = $this->StatDailyTradeChannelDao->getWhere([
                'mchId'     => $mch['id'],
                'channelId' => $channel['id'],
                'statDate' => $statDate,
                'delFlag' => 0
            ]);

            if ($isForce && $isStat) {
                // 强制成功且有数据就删除
                $this->StatDailyTradeChannelDao->del($isStat['id']);
            } else if (!$isForce && $isStat) {
                // 有数据就不管了
                continue;
            }

            // 取数据
            $sum = $this->TradeOrderDetailDao->getSumByTransStatusAndStartTimeAndEndTime($mch['id'], $channel['id'], [6], $startTime, $endTime);
            $sumFee = $this->TradeOrderDetailDao->getSumByTransStatusAndStartTimeAndEndTime($mch['id'], $channel['id'], [], $startTime, $endTime);

            // 写统计表
            $data = [
                'id'                     => CustomUtils::createUUID(),
                'mchId'                  => $mch['id'],
                'mchNo'                  => $mch['mchNo'],
                'mchName'                => $mch['mchName'],
                'channelId'              => $channel['id'],
                'channelName'            => $channel['channelName'],
                'settleCurrency'         => $config['settleCurrency'],

                'settleAmount'           => isset($sum['settleAmount']) ? $sum['settleAmount'] : 0,
                'serviceFee'             => isset($sumFee['serviceFee']) ? $sumFee['serviceFee'] : 0,

                'salesLevel1'            => $parentSalesId,
                'salesLevel2'            => $mch['salesId'],

                'salesCommission1'       => isset($sum['salesCommission1']) ? $sum['salesCommission1'] : 0,
                'salesCommission2'       => isset($sum['salesCommission2']) ? $sum['salesCommission2'] : 0,
                'channelSettleCurrency'  => $channel['settleCurrency'],

                'statDate'              => $statDate,
                'statStartTime'          => $startTime,
                'statEndTime'            => $endTime,
                'delFlag'                => 0,
                'createTime'             => time()
            ];
            $this->StatDailyTradeChannelDao->add($data);
        }
    }
}